package uk.org.landeg.euler.problems;

import java.util.Collection;
import java.util.Vector;

import uk.org.landeg.euler.problems.framework.AbstractProblem;
import uk.org.landeg.euler.problems.framework.ProblemLauncher;
import uk.org.landeg.euler.problems.lib.NumbersLib;

public class Problem021 extends AbstractProblem{
	public static void main(String[] args) {
		ProblemLauncher launcher = new ProblemLauncher(new Problem021());
		launcher.solve();		
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return "Evaluate the sum of all the amicable numbers under 10000";
	}

	@Override
	public Integer getProblemNumber() {
		// TODO Auto-generated method stub
		return 21;
	}

	private long sum (Collection<Long> c) {
		Long total = 0L;
		for (Long l : c) {
			total += l;
		}
		return total;
	}
	
	@Override
	public Object solve() {
		long pair;
		long total = 0;
		notes.add("D(200) = " + NumbersLib.getDivisors(220));
		Vector<Long> discovered = new Vector<Long> ();
		for (int n = 2 ; n < 10000 ; n++) {
			if (!discovered.contains((long)n)) {
				pair = sum(NumbersLib.getDivisors(n));
				if (pair != n && sum(NumbersLib.getDivisors(pair)) == n) {
					notes.add("Pair discovered : " + n + "\t"+ pair);
					total += pair + n;
					discovered.add(pair);
					discovered.add((long)n);
				}
			}
		}
		return total;
	}


}
